package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    /**
     * 根据用户ID查询作者
     *
     * @param userId
     * @return
     */
    @Override
    public ApAuthor findByUserId(Integer userId) {
        //检查参数
        if (userId == null || userId == 0) {
            return null;
        }

        //LambdaQueryWrapper<ApAuthor> wrapper = Wrappers.<ApAuthor>lambdaQuery();
        //wrapper.eq(ApAuthor::getUserId, userId);
        //根据ID查询作者
        ApAuthor apAuthor = getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));

        return apAuthor;
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        //检查参数
        if (apAuthor == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //保存作者
        save(apAuthor);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}